<?php
class model_advs extends base_model{
    //取出指定分类
    public function mod_advs_category_item($id){
        $sql = "SELECT * FROM `@@adv_category` WHERE `id`=".$id;
        return $this->db->query($sql,2);
    }

    //取出所有分类用作下拉列表
    public function mod_advs_category_list(){
        $sql = "SELECT `id`,`title` FROM `@@adv_category`";
        return $this->db->query($sql,3);
    }
    /*
	* 取出所有的分类
	*/
    public function mod_advs_category($args){
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title` LIKE '%{$args['sKey']}%'";
        }

        $sql = "SELECT ~field~ FROM `@@adv_category` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => '',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
    /*
    * 取出指定（搜索）子分类
    * @param int $where
    * @param string $field 要返回的字段列表
    * return array
    */
    public function mod_product_category_sub($where,$field='id,title'){
        $sql = "SELECT {$field} FROM `@@product_category` WHERE 1 {$where} order by `parentid`,`sort` ASC";
        $data = $this->db->query($sql,3);
        return $data;
    }
    /*
	* 取出所有的分类
	*/
    public function mod_product_category_list(){
        $sql = "SELECT id,title FROM `@@product_category` WHERE `parentid`=0";
        $data = $this->db->query($sql,3);
        foreach($data as $k=>$v){
            $subwhere = ' AND `parentid`='.intval($v['id']);
            $data[$k]['subdata'] = $this->mod_product_category_sub($subwhere);
            $subwhere = '';
        }
        return $data;
    }
    //取出指定广告
    public function mod_advs_index_item($id){
        $sql = "SELECT * FROM `@@advs` WHERE `id`={$id} LIMIT 1";
        return $this->db->query($sql,2);
    }
    //取出所有的广告
    public function mod_advs_items($args){
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title` LIKE '%{$args['sKey']}%'";
        }
        if(!empty($args['categoryid'])){
            $where_txt .= " AND `categoryid`='{$args['categoryid']}'";
        }
        $sql = "SELECT ~field~ FROM `@@advs` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => ' ORDER BY `id` DESC',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
	
}
?>